package com.timeriver.machine_learning.regression

import org.apache.spark.ml.PipelineModel
import org.apache.spark.ml.feature.LabeledPoint
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

object DecisionTreePredict {
  def main(args: Array[String]): Unit = {
    val session: SparkSession = SparkSession.builder()
      .master("local[6]")
      .appName("决策树回归模型预测")
      .getOrCreate()

    import session.implicits._

    val ds: Dataset[String] = session.read
      .textFile("D:\\workspace\\gitee_space\\spark-ml-machine-learning\\data\\housing.data")

    val value: Dataset[LabeledPoint] = ds.map(_.trim).filter(line => !line.isEmpty)
      .map(line => {
        val array: Array[Double] = line.split("\\s+").map(_.toDouble)
        LabeledPoint(
          -1,
          Vectors.dense(array.slice(1, array.size-1))
        )
      })

    val model: PipelineModel = PipelineModel.load("./model/decisiontreeregression")

    val frame: DataFrame = model.transform(value)

    frame.show(5, false)

    session.stop()
  }
}
